package com.studentmanagement.util;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 图标工具类，提供应用内使用的图标
 */
public class IconUtil {
    
    /**
     * 获取用户图标
     */
    public static Icon getUserIcon() {
        return new ImageIcon(createUserIcon(18, 18));
    }
    
    /**
     * 获取锁图标
     */
    public static Icon getLockIcon() {
        return new ImageIcon(createLockIcon(18, 18));
    }
    
    /**
     * 获取退出图标
     */
    public static Icon getExitIcon() {
        return new ImageIcon(createExitIcon(48, 48));
    }
    
    /**
     * 创建用户图标
     */
    private static Image createUserIcon(int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        
        try {
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            // 透明背景
            g2d.setColor(new Color(0, 0, 0, 0));
            g2d.fillRect(0, 0, width, height);
            
            // 绘制头部
            g2d.setColor(new Color(52, 152, 219));
            int headSize = width / 2;
            int headX = (width - headSize) / 2;
            int headY = height / 5;
            g2d.fillOval(headX, headY, headSize, headSize);
            
            // 绘制身体
            int bodyWidth = width / 2 + 2;
            int bodyHeight = height / 2;
            int bodyX = (width - bodyWidth) / 2;
            int bodyY = headY + headSize - 2;
            g2d.fillArc(bodyX, bodyY, bodyWidth, bodyHeight, 0, 180);
        } finally {
            g2d.dispose();
        }
        
        return image;
    }
    
    /**
     * 创建锁图标
     */
    private static Image createLockIcon(int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        
        try {
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            // 透明背景
            g2d.setColor(new Color(0, 0, 0, 0));
            g2d.fillRect(0, 0, width, height);
            
            // 绘制锁主体
            g2d.setColor(new Color(52, 152, 219));
            int lockWidth = width * 2 / 3;
            int lockHeight = height * 2 / 3;
            int lockX = (width - lockWidth) / 2;
            int lockY = height / 3;
            g2d.fillRoundRect(lockX, lockY, lockWidth, lockHeight, 3, 3);
            
            // 绘制锁环
            int arcWidth = lockWidth * 2 / 3;
            int arcHeight = height / 3;
            int arcX = (width - arcWidth) / 2;
            int arcY = lockY - arcHeight / 2;
            g2d.setStroke(new BasicStroke(2));
            g2d.drawArc(arcX, arcY, arcWidth, arcHeight, 0, 180);
        } finally {
            g2d.dispose();
        }
        
        return image;
    }
    
    /**
     * 创建退出图标
     */
    private static Image createExitIcon(int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        
        try {
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            // 透明背景
            g2d.setColor(new Color(0, 0, 0, 0));
            g2d.fillRect(0, 0, width, height);
            
            // 绘制门的轮廓
            g2d.setColor(new Color(52, 152, 219));
            int doorWidth = width * 3 / 5;
            int doorHeight = height * 4 / 5;
            int doorX = width / 5;
            int doorY = height / 10;
            
            // 绘制门体
            g2d.fillRoundRect(doorX, doorY, doorWidth, doorHeight, 5, 5);
            
            // 绘制门把手
            g2d.setColor(new Color(236, 240, 241));
            int handleSize = width / 10;
            int handleX = doorX + doorWidth - handleSize * 2;
            int handleY = doorY + doorHeight / 2 - handleSize / 2;
            g2d.fillOval(handleX, handleY, handleSize, handleSize);
            
            // 绘制退出箭头
            g2d.setColor(new Color(231, 76, 60));
            g2d.setStroke(new BasicStroke(3));
            
            // 箭头主体
            int arrowLength = width / 3;
            int arrowX1 = doorX + doorWidth + 5;
            int arrowX2 = arrowX1 + arrowLength;
            int arrowY = doorY + doorHeight / 2;
            g2d.drawLine(arrowX1, arrowY, arrowX2, arrowY);
            
            // 箭头头部
            int arrowHeadSize = width / 8;
            int[] xPoints = {arrowX2, arrowX2 - arrowHeadSize, arrowX2 - arrowHeadSize};
            int[] yPoints = {arrowY, arrowY - arrowHeadSize / 2, arrowY + arrowHeadSize / 2};
            g2d.fillPolygon(xPoints, yPoints, 3);
            
        } finally {
            g2d.dispose();
        }
        
        return image;
    }
} 